﻿$(document).ready(function () {


    $("#finish").click(function () {
        loading('stock');

        var stockcode = $("#stockcode").val();
        var year = $("#year-drop").find("option:selected").val();
        var yearText = $("#year-drop").find("option:selected").text();
        var quarter = $("#quarter-drop").find("option:selected").val();
        var quarterText = $("#quarter-drop").find("option:selected").text();
        var forceastYear = $("#forceastYear-drop").find("option:selected").val();
        var detailYearText = $("#detailYear-drop").find("option:selected").text();
        var unit = $("#unit-drop").find("option:selected").val();
        var unitText = $("#unit-drop").find("option:selected").text();

        if ($.trim(stockcode).length == 0) {
            removeLoading('stock');
            showMessage("股票代码不能为空");
            return;
        }

        request($.trim(stockcode), year, quarter, forceastYear, unit);
    })

    function request(code, year, quarter, forceastYear, unit) {

        var params = JSON.parse("{}");
        params.year = year;
        params.quarter = quarter;
        params.forceastYear = forceastYear;
        params.unit = unit;

        //1.目录sheet
        var categoryUrl = kURL_Category + "?code=" + code + "&unit=" + unit;
        //2.年报sheet
        var annualUrl = kURL_Annual + "?code=" + code + "&unit=" + unit + "&nums=" + year;
        //3.季报sheet
        var quarterUrl = kURL_Quarter + "?code=" + code + "&unit=" + unit + "&nums=" + quarter;
        //4.单季报sheet
        var singleQuarterUrl = kURL_SingleQuarter + "?code=" + code + "&unit=" + unit + "&nums=" + quarter;
        //5.主营业务构成sheet
        var mainrevenuedetailUrl = kURL_Mainrevenuedetail + "?code=" + code + "&unit=" + unit + "&nums=" + year;
         
        var category;
        var annual;
        var quarter;
        var singleQuarter;
        var mainrevenuedetail;
         
        $.getJSON(categoryUrl).then(function (json) {
            if (json.code == 200) {
                category = json.data;
                return $.getJSON(annualUrl);
            } else {
                throw json.msg;
            }
        }).then(function (json) {
            if (json.code == 200) {
                annual = json.data;
                return $.getJSON(quarterUrl);
            } else {
                throw json.msg;
            }
        }).then(function (json) {
            if (json.code == 200) {
                quarter = json.data;
                return $.getJSON(singleQuarterUrl);
            } else {
                throw json.msg;
            }
        }).then(function (json) {
            if (json.code == 200) {
                singleQuarter = json.data;
                return $.getJSON(mainrevenuedetailUrl);
            } else {
                throw json.msg;
            }
        }).then(function (json) {
            if (json.code == 200) {
                mainrevenuedetail = json.data;
                return null;
            } else {
                throw json.msg;
            }
        }).then(function (j) {
            var datas = JSON.parse("{}");
            datas.category = category;
            datas.annual = annual;
            datas.quarter = quarter;
            datas.singleQuarter = singleQuarter;
            datas.mainrevenuedetail = mainrevenuedetail;
             
            var result = JSON.parse("{}");
            result.params = params;
            result.datas = datas;

            Office.context.ui.messageParent(JSON.stringify(result));

         }).catch(function (jqXHR, textStatus, errorThrown) {
             console.log(jqXHR);
             removeLoading('stock');
             if (typeof (jqXHR) == "string") {
                 showMessage(jqXHR);
             } else {
                 showMessage("网络连接失败，请检查网络");
             } 
        });

    }


}); 

